import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:multi_image_picker_view/multi_image_picker_view.dart';
import 'package:qianyi/controller/homeTabController.dart';

import '../custom_examples.dart';
import '../picker.dart';

class FullCustomExample extends StatefulWidget {
  const FullCustomExample({Key? key}) : super(key: key);

  @override
  State<FullCustomExample> createState() => _FullCustomExampleState();
}

class _FullCustomExampleState extends State<FullCustomExample> {
  HomeTabController homeTabController = Get.find<HomeTabController>();
  final controller = MultiImagePickerController(
    maxImages: 9,
    picker: (bool allowMultiple) async {

      return await pickImagesUsingImagePicker(allowMultiple);
    },
  );
  @override
  void initState() {
    // TODO: implement initState
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset:false,

      appBar: AppBar(
        leading: Text(""),
        title: Container(

            child:Text("添加图片",style: TextStyle(
              fontWeight: FontWeight.w300,
              fontSize: 16,

            ),),

        )

      ),
      body: MultiImagePickerView(

        controller: controller,
        draggable: true,
        longPressDelayMilliseconds: 250,
        onDragBoxDecoration: BoxDecoration(
          borderRadius: BorderRadius.circular(8),
          boxShadow: [
            BoxShadow(
              color: Theme.of(context).colorScheme.shadow.withOpacity(0.5),
              blurRadius: 5,
            ),
          ],
        ),
        shrinkWrap: false,
        padding: const EdgeInsets.all(0),
        gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
          maxCrossAxisExtent: 90,
          childAspectRatio: 0.8,
          crossAxisSpacing: 2,
          mainAxisSpacing: 2,
        ),
        builder: (context, imageFile) {
          return Stack(
            children: [
              Positioned.fill(child: ImageFileView(imageFile: imageFile)),
              Positioned(
                  top: 4,
                  right: 4,
                  child: DraggableItemInkWell(
                    borderRadius: BorderRadius.circular(2),
                    onPressed: () => controller.removeImage(imageFile),
                    child: Container(
                        padding: const EdgeInsets.all(5),
                        decoration: BoxDecoration(
                          color: Theme.of(context)
                              .colorScheme
                              .secondary
                              .withOpacity(0.4),
                          shape: BoxShape.circle,
                        ),
                        child: Icon(
                          Icons.delete_forever_rounded,
                          size: 18,
                          color: Theme.of(context).colorScheme.surface,
                        )),
                  )),
            ],
          );
        },
        initialWidget: SizedBox(

          height: 170,
          width: double.infinity,
          child: Center(
            child: TextButton(
              style: TextButton.styleFrom(
                foregroundColor: Colors.red,
                surfaceTintColor: Colors.red,
                backgroundColor: Colors.pinkAccent.withOpacity(0.2),
                shape: const CircleBorder(),
              ),
              onPressed: () {
                controller.pickImages();
                homeTabController.imageController = controller;

              }
              ,
              child: const Padding(
                padding: EdgeInsets.all(10),
                child: Icon(
                  Icons.add,
                  size: 30,
                  // color: Colors.pinkAccent,
                ),
              ),
            )


          ),
        ),
        addMoreButton: SizedBox(
          height: 160,
          width: double.infinity,
          child: Center(
            child: TextButton(
              style: TextButton.styleFrom(
                foregroundColor: Colors.red,
                surfaceTintColor: Colors.red,
                backgroundColor: Colors.pinkAccent.withOpacity(0.2),
                shape: const CircleBorder(),
              ),
              onPressed: controller.pickImages,
              child: const Padding(
                padding: EdgeInsets.all(10),
                child: Icon(
                  Icons.add,
                  size: 30,
                  // color: Colors.pinkAccent,
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}
